
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
	if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="Admin for Online Token Sharing game!"');
    header('HTTP/1.0 401 Unauthorized');
    echo '<div id="doc" style="width:760px; padding-left:0px; margin-left:0px">
	   <div id="bd">
	   		<div id="yui-main">
				<div class="yui-b">
					<div class="yui-g">
						<div class="pageHeading" style="padding:15px 10px 15px 0px;">Online Token Sharing Game !!</div>
                        <br/>
                        <div class="sectionHeading">Authentication Invalid</div>
						<div style="padding:0px 0px 15px 0px;">
							<p class="font4">You are not authorized to view this page. The username/password your have entered is incorrect.</p>
						</div>
					</div>
				</div>
	   		</div>
	   </div>
	   <div id="test">
	   </div>
	   </div>';
    exit;
} else {
	$username = $_SERVER['PHP_AUTH_USER'];
    $password = $_SERVER['PHP_AUTH_PW'];
    if($username != 'admin' || $password != 'admin'){
    	die('<div id="doc" style="width:760px; padding-left:0px; margin-left:0px">
	   	<div id="bd">
	   		<div id="yui-main">
				<div class="yui-b">
					<div class="yui-g">
						<div class="pageHeading" style="padding:15px 10px 15px 0px;">Online Token Sharing Game !!</div>
                        <br/>
                        <div class="sectionHeading">Authentication Invalid</div>
						<div style="padding:0px 0px 15px 0px;">
							<p class="font4">You are not authorized to view this page. The username/password your have entered is incorrect.</p>
						</div>
					</div>
				</div>
	   		</div>
	   </div>
	   <div id="test">
	   </div>
		</div>');
    	exit;
    }
}?>
<!--block_head-->
<link href="App_Themes/Intel.com/grids-min.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<link href="App_Themes/Intel.com/v3.css" rel="stylesheet" type="text/css" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<!--object-specific stylesheets-->
<link href="App_Themes/Intel.com/downloads.css" rel="stylesheet" type="text/css">

<script language="javascript" src="App_Themes/scripts/tables.js"  type="text/javascript" xmlns:msxsl="urn:schemas-microsoft-com:xslt"></script>

<script type="text/javascript" src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php"></script>

<script language="javascript" src="App_Themes/scripts/scripts.js"  type="text/javascript" xmlns:msxsl="urn:schemas-microsoft-com:xslt"></script>
<script type="text/javascript" src="_/js/myscript.js"></script>


<!--/object-specific stylesheets-->
<!--/block_head-->

<!--  YUI Tables -->
	<!--CSS file (default YUI Sam Skin) -->
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.8.0r4/build/datatable/assets/skins/sam/datatable.css">

<!-- Dependencies -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script src="http://yui.yahooapis.com/2.8.0r4/build/element/element-min.js"></script>
<script src="http://yui.yahooapis.com/2.8.0r4/build/datasource/datasource-min.js"></script>

<!-- OPTIONAL: JSON Utility (for DataSource) -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/json/json-min.js"></script>

<!-- OPTIONAL: Connection Manager (enables XHR for DataSource) -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/connection/connection-min.js"></script>

<!-- OPTIONAL: Get Utility (enables dynamic script nodes for DataSource) -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/get/get-min.js"></script>

<!-- OPTIONAL: Drag Drop (enables resizeable or reorderable columns) -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/dragdrop/dragdrop-min.js"></script>

<!-- OPTIONAL: Calendar (enables calendar editors) -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/calendar/calendar-min.js"></script>

<!-- Source files -->
<script src="http://yui.yahooapis.com/2.8.0r4/build/datatable/datatable-min.js"></script>

<!--  /YUI Tables -->
<?php
    require_once("database.php");
    require_once("facebook_include1.php");
    require_once("./config.php");
    $me = $facebook->api('/me');
    $access_token = $facebook->getAccessToken();
    $user = $facebook->getUser();
    //echo $user ;
?>
<?php
	
        login($access_token);
	$currentExpiry = -1;
	$maxSignUps = -1;
?>

<?php
	if(isset($_GET['expiryHours'])){
		$hours = intval($_GET['expiryHours']);
		$file = "/home/files/expiry.txt";
		$fh = fopen($file, 'w');
		fwrite($fh, $hours);
		fclose($fh);
		$currentExpiry = $hours;
	}else if(isset($_GET['maxSignUps'])){
		//echo "test";
		$maxSignUps = intval($_GET['maxSignUps']);
		$file = "/home/files/maxsignup.txt";
		$fh = fopen($file, 'w');
		fwrite($fh, $maxSignUps);
		fclose($fh);
	}else if (isset($_GET['userid1'])){
		$userid1 = explode(",",$_GET['userid1']);
		$userid2 = explode(",",$_GET['userid2']);
		$i = 0;
        	$treatmentid = intval(getMaxTreatmentId()) + 1;
		for($i = 0; $i < count($userid1); $i++ ){
			addPair($userid1[$i], $userid2[$i], 1, null, $treatmentid, null);
		}
	}else if(isset($_GET['sender'])){
		//echo "test";
                $number = intval($_GET['Snumber']);
                $treatmentid = intval(getMaxTreatmentId()) + 1;
                for($i = 0; $i < $number; $i++){
                $userNotPaired = getUnpairedUsers();
                $length = count($userNotPaired);
                if($length < 1)
                    break;
                srand(time());
                $random = (rand()%$length);
                $userid1 = $userNotPaired[$random]['id'];
                $userid2 = 0;
                addPair($userid1, $userid2,1, true, $treatmentid, null);
                }
                
                
	}
        
        else if(isset($_GET['grouping'])){
		$group = $_GET['grouping'];
		$number = intval($_GET['number']);
		$anonymous = false;
		$treatmentid = intval(getMaxTreatmentId()) + 1;
		if(isset($_GET['anonymity']))
			$anonymous = true;
		if($group == 'any'){
			//Group users randomly
			for($i = 0; $i < $number; $i++){
				//echo $i . '<br />';
				$userNotPaired = getUnpairedUsers();
				$length = count($userNotPaired);
				if($length < 2)
					break;
				srand(time());
				$random = (rand()%$length);
				$userid1 = $userNotPaired[$random]['id'];
				array_splice($userNotPaired, $random, 1);
				$length--;
				srand(time());
				$random = (rand()%$length);
				$userid2 = $userNotPaired[$random]['id'];
				addPair($userid1, $userid2,1, $anonymous, $treatmentid, null);
			}
		}else if($group == 'friends' || $group == 'friendsWithMutual'){
			echo "came into friendsWithMutual\n";
                        $i=0;
			$userNotPaired = getUnpairedUsers();
            		$treatmentid = intval(getMaxTreatmentId()) + 1;
			while($i < $number){
				//echo $i . ":" . $number;
				$length = count($userNotPaired);
				$relation = 2;
				if($group == 'friendsWithMutual')
					$relation = 3;

				//echo "length:" . $length;
				if($length < 2)
					break;
				srand(time());
				$random = (rand()%$length);
				$userid1 = $userNotPaired[$random]['id'];
				//echo 'Userid1:' . $userid1. '<br/>';
				$friendsNotPaired = getFriendsNotPaired($userid1);
				$friendslength = count($friendsNotPaired);
				array_splice($userNotPaired, $random, 1);
				if($friendslength < 1){
					continue;
				}
				$random = (rand()%$friendslength);
				$userid2 = $friendsNotPaired[$random];
				$mutualfriends = getMutualFriendsFromDatabase($userid1, $userid2);
				$j = 0;
				for($j = 0; $j < count($userNotPaired); $j++ ){
					if($userNotPaired[$j]['id'] == $userid2)
						break;
				}
				if(($group == 'friends' && $mutualfriends <=0) || ($group == 'friendsWithMutual' && $mutualfriends > 0 )){
					if($j != count($userNotPaired)){
						array_splice($userNotPaired, $j, 1);
					}
					addPair($userid1, $userid2, $relation, $anonymous, $treatmentid, null);
					$i++;
				}
			}
		}else if($group == 'one_degree'){
			//Group one degree friends
            $treatmentid = intval(getMaxTreatmentId()) + 1;
			$userNotPaired = getUnpairedUsers();
			$i = 0;
			while($i < $number){
                        
				$length = count($userNotPaired);
				if($length < 1)
                                {
                                        break;
                                }
				srand(time());
				$userid1Index = (rand()%$length);
				$userid1 = $userNotPaired[$userid1Index]['id'];
                                //echo 'I broke out of the loop Userid1:' . $userid1. '<br/>';
				$friends = getFriendsinAppOneDegree($userid1);
                                
				while(true){
					$friendslength = count($friends);
					if($friendslength < 1){
						array_splice($userNotPaired, $userid1Index, 1);
						break;
					}
					srand(time());
                                        //echo 'Userid1:' . $userid1. '<br/>';
					$friendsIndex = (rand()%$friendslength);
					$friend = $friends[$friendsIndex];
					$oneDegreeFriends = getOneDegreeFriends($userid1, $friend);
                                        //echo "I returned out of this <br/>";
					if(count($oneDegreeFriends) <= 0){
						array_splice($friends, $friendsIndex, 1);
						continue;
					}
					$oneDegreeLength = count($oneDegreeFriends);
					srand(time());
					$userid2Index = (rand()% $oneDegreeLength);
                                        $userid2 = $oneDegreeFriends[$userid2Index];

                                        if ($userid2 != $userid1 )
                                        {
                                            //echo 'Userid2:' . $userid2. '<br/>';
                                            addPair($userid1, $userid2, 4, $anonymous, $treatmentid, $friend);
                                            $j=0;
                                            for($j=count($userNotPaired)-1; $j>=0; $j-- ){
                                            
                                                    if($userNotPaired[$j]['id'] == $userid1 || $userNotPaired[$j]['id'] == $userid2)
                                                    {
                                                            //echo "I removed ".$userNotPaired[$j]['id'].'<br/>';
                                                            array_splice($userNotPaired, $j, 1);
                                                    }
                                            }
                                            $i++;
                                            break;
                                        }
                                        
				}
			}
		}
	}

	if($currentExpiry <= 0 ){
		$file = "/home/files/expiry.txt";
		$fh = fopen($file, 'r');
		$contents = fgets($fh);
		fclose($fh);
		$currentExpiry = intval($contents);
	}
	if($maxSignUps <= 0){
		$file = "/home/files/maxsignup.txt";
		$fh = fopen($file, 'r');
		$contents = fgets($fh);
		fclose($fh);
		$maxSignUps = intval($contents);
	}
	if(isset($_GET['userDeleteId'])){
		$userids = explode(",",$_GET['userDeleteId']);
		//print_r($userid);
		deleteUsers($userids);
	}
	$users = getUsers();
	$usersNotPaired = getUnpairedUsers();
	//print_r($usersNotPaired);
	//echo sizeof($userNotPaired);
?>

</head>
<body>
<div id="doc" style="width:760px; padding-left:0px; margin-left:0px">
	   <div id="bd">
	   		<div id="yui-main">
				<div class="yui-b">
					<div class="pageHeading" style="padding:15px 10px 15px 0px;">Online Token Sharing Game - Admin </div>
					<div class="sectionHeading">
                    		<strong>No of Users: <span id="number"></span></strong>
                    </div>
                    <br/>
                    <div class="sectionHeading">
                    		<strong>No of Users Not Paired: <?php echo count($usersNotPaired);?></strong>
                    </div>
                    <br/>
					<div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Pair Users</strong>
                    	</div>
						<p class="font4">Select two users from the left table and add them to the right. Once you click on submit the system will choose the reciever and sender randomly from the pair and send them mails.</p>
                    </div>

                    <div class="fullWidth">
                        <div class="searchTable">
                        	<div id="componentsTable" style="height:420px; overflow-x:hidden; overflow-y:auto" class="borderClass">
                                <div class="yui-skin-sam font5">
                                	<div id="userTable"></div>
                                </div>
                            </div>
                        </div>
                        <div class="tableButtons" style="z-index:-10">
                            <table cellspacing="0">
	                            <tr class="middle">
	                                <td colspan="3">&nbsp;</td>
	                            </tr>
	                    	</table>
	                    	<input type="button" name="add" id="add" value="    Add &gt;    " class="search" style="margin-left:-6.5px" onclick="javascript:addPair();"/>
	                        <input type="button" name="remove" id="remove" value="&lt; Remove" class="search" style="margin-left:-6.5px" onclick="javascript:removePair();"/>
	                    </div>
                        <div class="cartTable">
                        	<div id="cartOuterContainer" class="borderClass" style="height:420px; overflow-x:hidden; overflow-y:auto">
                            	<div class="yui-skin-sam font5">
                            		<div id="pairTable"></div>
                            	</div>
							</div>
						</div>
                    </div>
                    <div class="yui-gb">
                    	<div class="yui-u first">
                    		&nbsp;
                    	</div>
                    	<div class="yui-u">
                    		&nbsp;
                    		<form id="pairs" name="pairs" action="admin.php" method="get">
                    			<input type="hidden" value="" name="userid1" id="userid1" />
                    			<input type="hidden" value="" name="userid2" id="userid2" />
                    		</form>
                    	</div>
                    	<div class="yui-u">
                    		<input type="button" name="submit" value="  Submit  "  class="search" onclick = "formSubmit();" />
                    		<div class="sectionBodyText font4"><span class="info" id="pairError" style="display:none">Please wait ...</span></div>
                    	</div>
                    </div>
                    <div class="sectionHeading">
                    	<strong>Randomly group users</strong>
                    </div>
                    <form id="random" name="random" method="get" action="admin.php">
                    	<strong class="sectionBodyText">
			                <div class="subSection">
			                    <strong class="sectionBodyText font4"><u>Relationship:</u></strong>
			                    <div class="yui-g">
			                    	<div class="yui-u first">
			                    		<div class="yui-g" style="width:100%">
			                    			<div class="yui-u first">
			                    				<input type="radio" value="any" name="grouping" id="any" /> <label for="any">Any relation</label>
			                    			</div>
			                    			<div class="yui-u">
			                    				<input type="radio" value="friends" name="grouping" id="friends" /> <label for="friends">Friends(Without Mutual Friends)</label>
			                    			</div>
			                    		</div>
			                    	</div>
			                    	<div class="yui-u">
			                    		<div class="yui-g" style="width:100%">
			                    			<div class="yui-u first">
			                    				<input type="radio" value="friendsWithMutual" name="grouping" id="friendsWithMutual" /> <label for="one_degree">Friends(With Mutual Friends)</label>
			                    			</div>
			                    			<div class="yui-u">
			                    				<input type="radio" value="one_degree" name="grouping" id="one_degree" /> <label for="one_degree">Friends of friends</label>
			                    			</div>
			                    		</div>
			                    	</div>
			                    </div>
			                </div>
		                    <br/>
		                    <div class="subSection">
		                    	<strong class="sectionBodyText font4"><u>Anonymity:</u></strong><br/>
                    			<input type="checkbox" name="anonymity" id="anonymous" /> <label for="anonymous">Anonymous</label>
			                </div>
		                    <div>
		                    	Enter the no. of pairs to be created : <input type="text" name="number" id="number" />
		                    </div>
		                    <br/>
	                    </strong>
                    </form>
                    <div class="yui-gb">
                    	<div class="yui-u first">
                    		&nbsp;
                    	</div>
                    	<div class="yui-u">
                    		&nbsp;
                    	</div>
                    	<div class="yui-u">
                    		<input type="button" name="submit_random" value="  Submit  "  class="search" onclick="submitRandomForm();" />
                    	</div>
                    </div>
                    
                    
                    
                    <div class="sectionHeading">
                    	<strong>Single user as Sender - No Reciever</strong>
                    </div>
                    <form id="SenderOnly" name="SenderOnly" method="get" action="admin.php">
                    	<strong class="sectionBodyText">
			                <div class="subSection">
			                    <strong class="sectionBodyText font4"><u>Sender:</u></strong>
			                    <div class="yui-g">
			                    	<div class="yui-u first">
			                    		<div class="yui-g" style="width:100%">
			                    			<div class="yui-u first">
			                    				<input type="radio" value="sender" name="sender" id="sender" /> <label for="sender">sender Only No reciever</label>
			                    			</div>
			                    		</div>
			                    	</div>
			                    </div>
			                </div>
		                    <br/>
		                    <div>
		                    	Enter the no. of pairs to be created : <input type="text" name="Snumber" id="Snumber" />
		                    </div>
		                    <br/>
	                    </strong>
                    </form>
                    <div class="yui-gb">
                    	<div class="yui-u first">
                    		&nbsp;
                    	</div>
                    	<div class="yui-u">
                    		&nbsp;
                    	</div>
                    	<div class="yui-u">
                    		<input type="button" name="submit_Sender" value="  Submit  "  class="search" onclick="submitSenderForm();" />
                    	</div>
                    </div>                    
                    
                    
                    
                    
                    <div class="sectionHeading">
                    	<strong>Validity</strong>
                    </div>
                    <div class="sectionBodyText">
                    	<form id="expiryTime" name="expiryTime" action="admin.php" methond="get">
                    		<strong class="sectionBodyText font4">Hours available</strong>
			            	<input type="text" id="expiryHours" name="expiryHours" value="<?php echo $currentExpiry; ?>"/>
			            </form>
                   	</div>
	               	<div class="yui-gb">
	                	<div class="yui-u first">
	                		&nbsp;
	                	</div>
	                	<div class="yui-u">
	                		&nbsp;
	                	</div>
	                	<div class="yui-u">
	                		<input type="button" name="submit_expiry" value="  Submit  "  class="search" onclick="submitExpiryForm();" />
	                	</div>
	                </div>
					<div class="sectionHeading">
                    	<strong>Maximum Users</strong>
                    </div>
                    <div class="sectionBodyText">
                    	<form id="maxSignUpsForm" name="maxSignUpsForm" action="admin.php" methond="get">
                    		<strong class="sectionBodyText font4">Maximum number of users allowed</strong>
			            	<input type="text" id="maxSignUps" name="maxSignUps" value="<?php echo $maxSignUps; ?>"/>
			            </form>
                   	</div>
	               	<div class="yui-gb">
	                	<div class="yui-u first">
	                		&nbsp;
	                	</div>
	                	<div class="yui-u">
	                		&nbsp;
	                	</div>
	                	<div class="yui-u">
	                		<input type="button" name="submit_maxsignups" value="  Submit  "  class="search" onclick="submitMaxSignUpForm();" />
	                	</div>
	                </div>
	                <div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Delete Users</strong>
                    	</div>
						<p class="font4">To delete users, select them from the list and click on delete.</p>
						<div class="yui-skin-sam font5">
							<div id = "userAdminTable">
							</div>
						</div>
						<div class="yui-gb">
		                	<div class="yui-u first">
	                			&nbsp;
		                	</div>
		                	<div class="yui-u">
		                		<form id = "userDelete" name="userDelete" action = "admin.php" method = "get">
		                			<input type="hidden" name="userDeleteId" id="userDeleteId" value="">
		                		</form>
		                		<input type="button" name="submit_delete" value="  Delete  "  class="search" onclick="deleteSelected();" />
		                	</div>
		                	<div class="yui-u">
		                		&nbsp;
		                	</div>
		                </div>
                    </div>
                    <div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Download Social Metrics</strong>
                    	</div>
                    	<form id="socialReportForm" name="socialReportForm" action = "download.php" method = "get">
                    		<input type="submit" name="socialReportDownload" value="  Download Social Report  "  class="search" />
                    	</form>
					</div>
					<br/>
					<div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Download Report</strong>
                    	</div>
						<form id="gameReportForm" name="gameReportForm" action = "download.php" method = "get">
                    		<input type="submit" name="gameReportDownload" value="  Download Game Report  "  class="search" />
                    	</form>
					</div>
					<br/>
					<div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Download Within Game Report</strong>
                    	</div>
						<form id="withinGameReportForm" name="withinGameReportForm" action = "download.php" method = "get">
                    		<input type="submit" name="withinGameReportDownload" value="  Download Within Game Report  "  class="search" />
                    	</form>
					</div>
					<br/>
					<div class="yui-g">
						<div class="sectionHeading">
                    		<strong>Download Payment Report</strong>
                    	</div>
						<form id="paymentReportForm" name="paymentReportForm" action = "download.php" method = "get">
                    		<input type="submit" name="paymentReportDownload" value="  Download Payment Report  "  class="search" />
                    	</form>
					</div>
					<br/>
                </div>
	   		</div>
	   </div>
</div>
<input type="hidden" id="withinAdding" name = "withinAdding" value="sender"/>
<script language="javascript">
	var usersNotPaired = new Array();
	<?php
		$i=0;
		foreach($usersNotPaired as $userNotPaired){
			echo "usersNotPaired[$i] = {id:'". $userNotPaired['id']. "', name:'" . $userNotPaired['name'] . "'};";
			$i++;
		}
	?>

	populateUsersTable(usersNotPaired);
	createPairsTable();

	populateUsersWithinTable(usersNotPaired);
	createPairsWithinTable();
</script>
<script language="javascript">
	var usersAdmin = new Array();
	<?php
		$i=0;
		foreach($users as $user){
			echo "usersAdmin[$i] = {id:'". $user['id']. "', email:'" . $user['email']. "', name:'" . $user['name'] . "'};";
			$i++;
		}
	?>
	createUsersAdminTable(usersAdmin);
	var userslength = usersAdmin.length;
	document.getElementById('number').innerHTML = userslength;
</script>
</body>
</html>
